package com.work.dao.impl;

import com.work.bean.User;
import com.work.dao.BaseDao;
import com.work.dao.UserDao;

import java.util.List;

public class UserDaoImpl extends BaseDao implements UserDao {
    public User queryUserByUsername(String uname) {
        String sql = "SELECT `id`, `uname`, `upass`, `type`,`rname` FROM `t_user` WHERE `uname` = ?";
        return queryForOne(User.class, sql, uname);
    }

    public User queryUserByUserNameAndPassWord(String uname, String uPass) {
        String sql = "SELECT `id`, `uname`, `upass`, `type`,`rname` FROM `t_user` WHERE `uname` = ? AND `upass` = ?";
        return queryForOne(User.class, sql, uname, uPass);
    }

    @Override
    public List<User> queryByType(String type) {
        String sql = "SELECT * FROM `t_user` WHERE `type` = ?";
        return queryForList(User.class, sql, type);
    }

    public int saveUser(User user) {
        String sql = "INSERT INTO `t_user` (`uname`, `upass`, `type`, `rname`) VALUES (?, ?, ?, ?);";
        return update(sql,user.getUname(),user.getUpass(),user.getType(),user.getRname());
    }

    @Override
    public int delete(Integer id) {
        String sql = "DELETE FROM `t_user` WHERE `id` = ?";
        return update(sql, id);
    }

    @Override
    public int update(User user) {
        String sql = "UPDATE t_user SET `upass`=? WHERE `id` = ?";
        return update(sql, user.getUpass(),user.getId());
    }
}
